package controller;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AddTourDAO;
import entity.Tour;

/**
 * Servlet implementation class AddTour
 */
@WebServlet("/AddTour")
public class AddTourServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddTourServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Tour tour = new Tour();
		Date startDate = null;
		Date endDate = null;
		
		tour.setTourName(request.getParameter("tourName"));
		tour.setPlace(request.getParameter("place"));
		String sd = request.getParameter("startDate");
		String ed = request.getParameter("endDate");
		SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
		try {
			startDate = (Date) sdf.parse(sd);
			endDate = (Date) sdf.parse(ed);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tour.setStartDate(startDate);
		tour.setEndDate(endDate);
		tour.setPrice(Float.parseFloat(request.getParameter("price")));
		tour.setImages(request.getParameter("images"));
		tour.setMaxVisitors(Short.parseShort(request.getParameter("maxVisitors")));
		tour.setCurrentVisitors(Short.parseShort(request.getParameter("currentVisitors")));
		tour.setTag(request.getParameter("tag"));
		tour.setDescription(request.getParameter("description"));
		if(AddTourDAO.Addtour(tour)){
            RequestDispatcher RequetsDispatcherObj=request.getRequestDispatcher("/login.jsp");
            RequetsDispatcherObj.forward(request, response);
    }else{
           
    }
	}

}
